package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author chenshuntao
 * @date 2025-07-20
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private RedisTemplate redisTemplate;

    private static final String  CACHE_KEY_PREFIX = "nursingPlan:all";

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        //1.查询计划
        NursingPlan nursingPlan = getById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        List<NursingProjectPlan> list1 = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        //2.查询项目计划
        List<NursingProjectPlanVo> list = list1.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setId(nursingProjectPlan.getId());
            nursingProjectPlanVo.setPlanId(Math.toIntExact(nursingProjectPlan.getPlanId()));
            nursingProjectPlanVo.setExecuteTime(nursingProjectPlan.getExecuteTime());
            nursingProjectPlanVo.setExecuteFrequency(nursingProjectPlan.getExecuteFrequency());
            nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
            nursingProjectPlanVo.setExecuteCycle(String.valueOf(nursingProjectPlan.getExecuteCycle()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        //1.插入计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);

        this.save(nursingPlan);
        //2.插入项目计划
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (projectPlans != null && projectPlans.size() > 0) {
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
        }
        boolean b = nursingProjectPlanService.saveBatch(projectPlans);
        redisTemplate.delete(CACHE_KEY_PREFIX);

        return b ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

        //1.看条件是否修改计划的项目
        if (!CollectionUtils.isEmpty(projectPlans)) {
            //删除计划的项目
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));
            //批量插入计划项目
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        //2.修改计划
        boolean b = this.updateById(nursingPlan);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return b ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanById(Integer id) {
        //删除计划中的项目
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        boolean b = removeById(id);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return b ? 1 : 0;
    }

    @Override
    public List<NursingPlan> getAll() {
        //查缓存
        List<NursingPlan> nursingPlans  = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if (nursingPlans != null) {
            return nursingPlans;
        }
        nursingPlans = nursingPlanMapper.getAll();
        //将数据同步到缓存中
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, nursingPlans);
        return nursingPlans;
    }
}
